import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Service2 {
    public static void main(String[] args) throws IOException{
        Service socketService = new Service();
        socketService.oneServer();
    }
    public  void oneServer(){
        try{
            ServerSocket server=null;
            try{
                server=new ServerSocket(5223);
                System.out.println("服务器启动成功！");
            }catch(Exception e) {
                System.out.println("没有启动监听！"+e);
            }
            Socket socket=null;
            try{
                socket=server.accept();
            }catch(Exception e) {
                System.out.println("Error."+e);
            }
            String line,line2;
            BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter writer=new PrintWriter(socket.getOutputStream());
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            line2=in.readLine();
            System.out.println("客户端:"+line2);

            //创建对象aes，并调用 AES 类中的解密方法
            AesEncodeUtil aes = new AesEncodeUtil();
            String line3 = new String();
            line3 = aes.decrypt(line2);
            //调用 MyDC 类计算值
            MyDC f = new MyDC();
            System.out.printf("%d",f.evaluate(line3));
            writer.println(Integer.toString(f.evaluate(line3)));
            line=br.readLine();
            while(!line.equals("end")){
                writer.println(line);
                writer.flush();
                System.out.println("客户端:"+in.readLine());
                System.out.println("服务器:"+Integer.toString(f.evaluate(in.readLine())));
                line=br.readLine();
            }
            writer.close();
            in.close();
            socket.close();
            server.close();
        }catch(Exception e) {
            System.out.println("Error."+e);
        }
    }
}
